package com.example.supplychainbackend.controller;


import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.quality.*;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.QualityService;
import com.example.supplychainbackend.tool.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/quality")
@AllArgsConstructor
public class QualityController {
    private final QualityService qualityService;

    //检验批增删改查
    @GetMapping("/inspectionBatch/list")
    public DataResponse<List<InspectionBatchDto>> listInspectionBatch(){
        List<InspectionBatchDto> res = qualityService.listInspectionBatch();
        return DataResponse.success(res);
    }

    @GetMapping ("/inspectionBatch/{id}")
    public DataResponse<InspectionBatchDto> getInspectionBatch(@PathVariable String id){
        InspectionBatchDto res = qualityService.getInspectionBatch(id);
        return DataResponse.success(res);
    }

    @PostMapping("/inspectionBatch")
    public DataResponse<String> createInspectionBatch(@RequestBody InspectionBatchDto form){
        String inspectionBatchNumber = qualityService.createInspectionBatch(form);
        return DataResponse.success(inspectionBatchNumber);
    }

    @PutMapping ("/inspectionBatch/{id}")
    public DataResponse<InspectionBatchDto> updateInspectionBatch(@PathVariable String id, @RequestBody InspectionBatchDto form){
        InspectionBatchDto res = qualityService.updateInspectionBatch(id, form);
        return DataResponse.success(res);
    }

    @DeleteMapping ("/inspectionBatch/{id}")
    public DataResponse<String> deleteInspectionBatch(@PathVariable String id){
        qualityService.deleteInspectionBatch(id);
        return DataResponse.success(id);
    }

    @PostMapping("/unqualified/{id}/chain")
    public DataResponse<UploadInfo> uploadUnqualified(@PathVariable String id){
        UploadInfo res = qualityService.uploadUnqualified(id);
        return DataResponse.success(res);
    }
    //不合格品（处理单）增删改查
    @GetMapping("/unqualified/list")
    public DataResponse<List<UnqualifiedDto>> listUnqualified(){
        List<UnqualifiedDto> res = qualityService.listUnqualified();
        return DataResponse.success(res);
    }

    @GetMapping ("/unqualified/{id}")
    public DataResponse<UnqualifiedDto> getUnqualified(@PathVariable String id){
        UnqualifiedDto res = qualityService.getUnqualified(id);
        return DataResponse.success(res);
    }

    @PostMapping("/unqualified")
    public DataResponse<String> createUnqualified(@RequestHeader("Authorization") String authHeader,
                                                  @RequestBody UnqualifiedDto form){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        String id = qualityService.createUnqualified(form);
        return DataResponse.success(id);
    }

    @PutMapping ("/unqualified/{id}")
    public DataResponse<UnqualifiedDto> updateUnqualified(@RequestHeader("Authorization") String authHeader,
                                                          @PathVariable String id, @RequestBody UnqualifiedDto form){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        UnqualifiedDto res = qualityService.updateUnqualified(id, form);
        return DataResponse.success(res);
    }

    @DeleteMapping ("/unqualified/{id}")
    public DataResponse<String> deleteUnqualified(@PathVariable String id){
        qualityService.deleteUnqualified(id);
        return DataResponse.success(id);
    }

    @PostMapping("/inspectionRequest/{id}/chain")
    public DataResponse<UploadInfo> uploadInspectionRequest(@PathVariable String id){
        UploadInfo res = qualityService.uploadInspectionRequest(id);
        return DataResponse.success(res);
    }
    @GetMapping("/inspectionRequest/list")
    public DataResponse<List<InspectionRequestListDto>> getInspectionRequestList(){
        List<InspectionRequestListDto> res = qualityService.getInspectionRequestList();
        return DataResponse.success(res);
    }

    @GetMapping("/inspectionRequest/{id}")
    public DataResponse<InspectionRequestDto> getInspectionRequestList(@PathVariable String id){
        InspectionRequestDto res = qualityService.getByIdInspectionRequest(id);
        return DataResponse.success(res);
    }

    @PostMapping("/inspectionRequest")
    public DataResponse<InspectionRequestDto> createInspectionRequest(@RequestHeader("Authorization") String authHeader,
                                                                      @RequestBody InspectionRequestDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        InspectionRequestDto res = qualityService.createInspectionRequest(dto);
        return DataResponse.success(res);
    }

    @PutMapping("/inspectionRequest/{id}")
    public DataResponse<InspectionRequestDto> updateInspectionRequest(@RequestHeader("Authorization") String authHeader,
                                                                      @PathVariable String id,@RequestBody InspectionRequestDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        InspectionRequestDto res = qualityService.updateInspectionRequest(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/inspectionRequest/{id}")
    public DataResponse<String> deleteInspectionRequest(@PathVariable String id){
        String res = qualityService.deleteInspectionRequest(id);
        return DataResponse.success(res);
    }

    @PostMapping("/inspectionOrder/{id}/chain")
    public DataResponse<UploadInfo> uploadInspectionOrder(@PathVariable String id){
        UploadInfo res = qualityService.uploadInspectionOrder(id);
        return DataResponse.success(res);
    }
    @GetMapping("/inspectionOrder/list")
    public DataResponse<List<InspectionOrderListDto>> getInspectionOrderList(){
        List<InspectionOrderListDto> res = qualityService.getInspectionOrderList();
        return DataResponse.success(res);
    }

    @GetMapping("/inspectionOrder/{id}")
    public DataResponse<InspectionOrderDto> getInspectionOrderList(@PathVariable String id){
        InspectionOrderDto res = qualityService.getByIdInspectionOrder(id);
        return DataResponse.success(res);
    }

    @PostMapping("/inspectionOrder")
    public DataResponse<InspectionOrderDto> createInspectionOrder(@RequestHeader("Authorization") String authHeader,
                                                                  @RequestBody InspectionOrderDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        InspectionOrderDto res = qualityService.createInspectionOrder(dto);
        return DataResponse.success(res);
    }

    @PutMapping("/inspectionOrder/{id}")
    public DataResponse<InspectionOrderDto> updateInspectionOrder(@RequestHeader("Authorization") String authHeader,
                                                                  @PathVariable String id,@RequestBody InspectionOrderDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        InspectionOrderDto res = qualityService.updateInspectionOrder(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/inspectionOrder/{id}")
    public DataResponse<String> deleteInspectionOrder(@PathVariable String id){
        String res = qualityService.deleteInspectionOrder(id);
        return DataResponse.success(res);
    }

    @PostMapping("/inspectionRequest/beginInspection/{id}")
    public DataResponse<String> beginInspection(@PathVariable String id){
        qualityService.beginInspectionRequest(id);
        return DataResponse.success(id);
    }
}
